//
//  NRWCarAlterView.m
//  New_Runway
//
//  Created by 邢毅 on 2018/4/25.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "NRWCarAlterView.h"

@implementation NRWCarAlterView
{
    NSString *_title;
    NSString *_messgae;
    NSString *_leftTitle;
    NSString *_rightTitle;
    BOOL _isShowCloseBtn;
 
    UIView *_alterBackView;
    UIView *_contentView;
    
    UILabel *_titleLabel;
    UILabel *_messageLabel;
    UIButton *_leftBtn;
    UIButton *_rightBtn;
    UIButton *_closeBtn;
    id<NRWCarAlterViewDelegate> __weak _delegate;
}

+ (instancetype)alterViewWithTitle:(NSString *)title
                           messgae:(NSString *)messgae
                         leftTitle:(NSString *)leftTitle
                        rightTitle:(NSString *)rightTitle
                      showCloseBtn:(BOOL)isShowCloseBtn
                          delegate:(id<NRWCarAlterViewDelegate>)delegate {
    
    return [[[self class] alloc] initWithTitle:title messgae:messgae leftTitle:leftTitle rightTitle:rightTitle showCloseBtn:isShowCloseBtn delegate:delegate];
}

+ (instancetype)alterViewWithTitle:(NSString *)title messgae:(NSString *)messgae leftTitle:(NSString *)leftTitle rightTitle:(NSString *)rightTitle delegate:(id<NRWCarAlterViewDelegate>)delegate {
    
    return [self alterViewWithTitle:title
                            messgae:messgae
                          leftTitle:leftTitle
                         rightTitle:rightTitle
                       showCloseBtn:NO delegate:delegate];
}

- (instancetype)initWithTitle:(NSString *)title
                      messgae:(NSString *)messgae
                    leftTitle:(NSString *)leftTitle
                   rightTitle:(NSString *)rightTitle
                 showCloseBtn:(BOOL)isShowCloseBtn
                     delegate:(id<NRWCarAlterViewDelegate>)delegate
{
    self = [super init];
    if (self) {
        
        self.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(outSideDidClick:)];
        [self addGestureRecognizer:tap];
        
        _title = title;
        _messgae = messgae;
        _leftTitle = leftTitle;
        _rightTitle = rightTitle;
        _isShowCloseBtn = isShowCloseBtn;
        _delegate = delegate;
        
        [self prepare];
    }
    return self;
}

- (void)prepare {
    
    _titleColor = _messageColor = getColorFromString(@"#1C1B1B");
    _leftBtnTitleColor = _rightBtnTitleColor = getColorFromString(@"#007AFF");
    
    _titleFont = kPingFangRegular17();
    _messageFont = kPingFangRegular14();
    _leftBtnFont = _rightBtnFont = kPingFangMedium16();
    
    _titleLines = _messageLines = 2;
    
}

- (void)setupUI {
    
    _alterBackView = DPView().setBackgroundColor(getColorFromString(@"E0E0E0"));
    _alterBackView.layer.cornerRadius = 13;
    _alterBackView.layer.masksToBounds = YES;
    _contentView = DPView().setBackgroundColor(kColorWhite());
    
    _titleLabel = DPLabel()
    .setFont(_titleFont)
    .setTextColor(_titleColor)
    .setText(_title)
    .setTextAlignment(NSTextAlignmentCenter);
    _titleLabel.numberOfLines = _titleLines;
    
    _messageLabel = DPLabel()
    .setFont(_messageFont)
    .setTextColor(_messageColor)
    .setText(_messgae)
    .setTextAlignment(NSTextAlignmentCenter);
    _messageLabel.numberOfLines = _messageLines;
    
    _leftBtn = DPButton()
    .setNormalTitle(_leftTitle)
    .setNormalTitleColor(_leftBtnTitleColor)
    .setTitleFont(_leftBtnFont)
    .setNormalBackgroundColor(kColorWhite())
    .addClickAction(self, @selector(leftBtnDidClick:));
    
    _rightBtn = DPButton()
    .setNormalTitle(_rightTitle)
    .setNormalTitleColor(_rightBtnTitleColor)
    .setTitleFont(_rightBtnFont)
    .setNormalBackgroundColor(kColorWhite())
    .addClickAction(self, @selector(rightBtnDidClick:));
    
    if (_isShowCloseBtn) {
        _closeBtn = DPButton()
        .setNormalImage(getImageFromName(_closeBtnImage))
        .addClickAction(self, @selector(closeBtnDidClick:));
        _contentView.addSubview(_closeBtn);
    }
    
    _contentView
    .addSubview(_titleLabel)
    .addSubview(_messageLabel);
    
    _alterBackView
    .addSubview(_contentView)
    .addSubview(_leftBtn)
    .addSubview(_rightBtn);
    
    self.addSubview(_alterBackView);
}

- (void)layoutUI {
    
    CGFloat alterWidth = Screen_width * 0.72;
    
    if ([_title stringByReplacingOccurrencesOfString:@" " withString:@""].length == 0) {//title为空
        
        _alterBackView.frame = CGRectMake(0, 0, alterWidth, 135);
        _alterBackView.center = self.center;
        _contentView.frame = CGRectMake(0, 0, _alterBackView.viewWidth, _alterBackView.viewHeight - 44);
        
        if ([_messgae stringByReplacingOccurrencesOfString:@" " withString:@""].length == 0) {//message为空
            
            //不需要布局contentView了  btn下面统一布局
        } else {//message不为空
            
            [_messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.center.equalTo(_contentView);
                make.left.equalTo(_contentView.mas_left).offset(25);
                make.right.equalTo(_contentView.mas_right).offset(-25);
            }];
        }
    } else {//title不为空
        
        if ([_messgae stringByReplacingOccurrencesOfString:@" " withString:@""].length == 0) {//message为空
            
            _alterBackView.frame = CGRectMake(0, 0, alterWidth, 135);
            _alterBackView.center = self.center;
            _contentView.frame = CGRectMake(0, 0, _alterBackView.viewWidth, _alterBackView.viewHeight - 44);
            
            [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.center.equalTo(_contentView);
                make.left.equalTo(_contentView.mas_left).offset(25);
                make.right.equalTo(_contentView.mas_right).offset(-25);
            }];
        } else {//都不为空
            
            [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(_contentView.mas_top).offset(20);
                make.left.equalTo(_contentView.mas_left).offset(25);
                make.right.equalTo(_contentView.mas_right).offset(-25);
            }];
            
            [_messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(_titleLabel.mas_bottom).offset(20);
                make.left.equalTo(_titleLabel.mas_left);
                make.right.equalTo(_titleLabel.mas_right);
            }];
            
            [_contentView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(_titleLabel.mas_top).offset(-20);
                make.bottom.equalTo(_messageLabel.mas_bottom).offset(20);
                make.width.equalTo(@(alterWidth));
            }];
            
            [_alterBackView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(_contentView.mas_top);
                make.left.equalTo(_contentView.mas_left);
                make.right.equalTo(_contentView.mas_right);
                make.height.equalTo(_contentView.mas_height).offset(44);
                make.center.equalTo(self);
            }];
        }
    }
    
    CGFloat btnWidth = alterWidth / 2.0 - 0.25;
    
    [_leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_alterBackView.mas_left);
        make.top.equalTo(_contentView.mas_bottom).offset(0.5);
        make.height.equalTo(@43.5);
        make.width.equalTo(@(btnWidth));
    }];
    
    [_rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_alterBackView.mas_right);
        make.top.equalTo(_leftBtn.mas_top);
        make.height.equalTo(@43.5);
        make.width.equalTo(@(btnWidth));
    }];
}

- (void)leftBtnDidClick:(UIButton *)sender {
    
    if ([_delegate respondsToSelector:@selector(alterView:didClickWithType:btnTitle:)]) {
        
        [_delegate alterView:self didClickWithType:kAlterViewButtonTypeLeft btnTitle:sender.titleLabel.text];
    }
    
    [self dismiss];
}

- (void)rightBtnDidClick:(UIButton *)sender {
    
    if ([_delegate respondsToSelector:@selector(alterView:didClickWithType:btnTitle:)]) {
        
        [_delegate alterView:self didClickWithType:kAlterViewButtonTypeRight btnTitle:sender.titleLabel.text];
    }
    
    [self dismiss];
}

- (void)closeBtnDidClick:(UIButton *)sender {
    
    if ([_delegate respondsToSelector:@selector(alterView:didClickWithType:btnTitle:)]) {
        
        [_delegate alterView:self didClickWithType:kAlterViewButtonTypeClose btnTitle:nil];
    }
    
    [self dismiss];
}

- (void)outSideDidClick:(UITapGestureRecognizer *)tap {
    
    [self dismiss];
}

- (void)show {

    UIView *keyView = [UIApplication sharedApplication].keyWindow;
    [keyView addSubview:self];
    self.frame = keyView.bounds;
    
    [self setupUI];
    
    [self layoutUI];
    
    self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0];
    _alterBackView.alpha = 0;
    
    [UIView animateWithDuration:0.25 animations:^{
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
        _alterBackView.alpha = 1;
    }];
    
}

- (void)dismiss {
    
    [UIView animateWithDuration:0.25 animations:^{
        _alterBackView.alpha = 0;
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0];
    } completion:^(BOOL finished) {
       
        [self removeFromSuperview];
    }];
}

@end
